<template>
  <layout>
    <!-- 顶部操作 -->
    <section slot="search">
      <custom-form
        ref="search-form"
        class="search-form"
        v-model="tableSearch"
        layout="inline"
        :FormList="searchColumns"
      >
        <a-button
          class="action-button"
          type="primary"
          @click="search()"
        >搜索</a-button>
      </custom-form>
      <!-- <a-input placeholder="产品名称或者编号" v-model="tableSearch.searchKey" allowClear style="width: 200px;" /> -->
    </section>

    <!-- 产品列表 -->
    <a-table
      :columns="tableColumns"
      :dataSource="tableData"
      :rowKey="record => record.id"
      :customRow="customTableRow"
      :pagination="false"
      bordered
    >
      <template
        slot="task_statu"
        slot-scope="row"
      >
        {{getLabel(row.task_statu, [], 'task_statu')}}
      </template>
      <template
        slot="online_time"
        slot-scope="row"
      >
        {{getTimeDiff(row.i_time, row.u_time)}}分钟
      </template>
      <!-- 按钮区 -->
      <template
        slot="action"
        slot-scope="row"
      >
        <a-button
          class="action-button"
          type="primary"
          auth="project.edit"
          @click="handleTableEdit(row)"
        >编辑</a-button>
        <a-popconfirm
          title="您确认要删除该条数据?"
          ok-text="确认"
          cancel-text="取消"
          @confirm="tableDel(row)"
        >
          <a-button
            class="action-button"
            auth="project.delete"
            type="danger"
          >删除</a-button>
        </a-popconfirm>
      </template>
    </a-table>

    <!-- 产品列表分页器 -->
    <a-pagination
      slot="paging"
      :pageSizeOptions="pageSizes"
      :total="total"
      showSizeChanger
      :pageSize="pageSize"
      :show-total="showTotal"
      v-model="currentPage"
      @showSizeChange="changePageSize"
      @change="changePage"
    >
    </a-pagination>

    <!-- 编辑产品数据 -->
    <a-modal
      :visible="visible"
      :title="visibleTitle"
      @ok="handleSubmit"
      @cancel="visible = false"
    >
      <custom-form
        ref="custom-form"
        class="custom-form"
        v-model="currentRow"
        :FormList="formColumns"
        :mode="formMode"
        layout="horizontal"
      ></custom-form>
    </a-modal>

  </layout>
</template>

<script lang="ts">
import { Mixins, Component, Watch, Vue } from 'vue-property-decorator'
import { State, Mutation, Getter, namespace } from 'vuex-class'
import EventsMixin from '@/mixin/events'
import { TableMixin, AuthMixin } from '@/mixin'
import moment from 'moment'

import { TableLayout } from '@/layouts'
import CustomForm from '@/components/src/Form'
import { userServices, IProject } from '@/common/services/user'

import { statusType } from '@/common/enum'
import { monitorServices } from '@/common/services/monitor'

@Component({
  components: {
    Layout: TableLayout,
    CustomForm
  }
})
export default class AdminList extends Mixins(
  EventsMixin,
  TableMixin,
  AuthMixin
) {
  // 产品列表搜索条件
  public tableSearch = {
    page: 1,
    pageSize: 15
  }

  // 产品列表表格信息
  public columns: ITable.IColumn[] = [
    {
      title: '用户编号',
      dataIndex: 'user_id',
      key: 'user_id',
      form: {
        type: 'a-input',
        placeholder: '请输入用户编号',
        isShowQuery: true
      }
    },
    {
      title: '行政区名称',
      dataIndex: 's_a_nam',
      key: 's_a_nam',
    },
    {
      title: '登录名',
      dataIndex: 'usr_act',
      key: 'usr_act',
      form: {
        type: 'a-input',
        placeholder: '请输入登录名',
        isShowQuery: true
      }
    },
    {
      title: '真实姓名',
      dataIndex: 'usr_nickname',
      key: 'usr_nickname',
      form: {
        type: 'a-input',
        placeholder: '请输入姓名',
        isShowQuery: true
      }
    },
    // {
    //   title: '当前在线时长',
    //   key: 'online_time',
    //   scopedSlots: { customRender: 'online_time' }
    // },
    {
      title: '当前在线时长',
      key: 'count_time',
      dataIndex: 'count_time',
    },
    {
      title: '登录时间',
      dataIndex: 'i_time',
      key: 'i_time',
      form: {
        type: 'a-input',
        placeholder: '请选择项目使用地区编号',
      }
    },
    {
      title: '更新时间',
      key: 'u_time',
      dataIndex: 'u_time',
      form: {
        type: 'a-input',
        placeholder: '请选择项目状态',
        dict: statusType
      },
    },
    
  ]

  public tableApi = {
    list: monitorServices.getLoginList
  }

  public mounted() {
    this.getData()
  }

  public async getData() {
    const res = await this.getTableList()
    // this.tableData = res.data || []
  }

  // 更新当前数据状态
  public handleUpdateStatus(val: number, row: any) {
    const status = val ? '1' : '2'
    this.$api
      .updateStore({
        sid: row.sid,
        gid: row.gid,
        zt: status
      })
      .then((res: Api.ResponseOptions) => {
        row.zt = status
        this.$message.success('更新成功')
      })
  }

  // 编辑前数据预处理
  public async handleTableEdit(row: any) {
    this.tableEdit(Object.assign({}, row))
  }

  public getTimeDiff(start: string, end: string) {
    return moment(end).diff(moment(start), 'minutes')
  }
}
</script>

<style scoped>
.action-button {
  margin: 0px 10px 0px 10px;
}
.form-item-class {
  margin-bottom: 0px;
}
.img-box {
  width: 100px;
  height: 100px;
  margin: 10px;
}
.item-blue {
  color: #1989fa;
}
</style>
